define(["jQuery"], function ($) {
    return {
		editMode : false,
		editedId : false,
		editedProp : false,
		frameMode : true,
		show : function(){
			$("#creation-pencil").trigger('click');
			$( "#shape-dragable-menu" ).show();
			var width = $("#draw_body").css('width');
			console.log(width.substring(0,width.length-2));
			width=(parseInt(width.substring(0,width.length-2))-250);
			$( "#shape-dragable-menu").css('left', width);
			$( "#shape-dragable-menu").css('top', 0);
			$(".shape-2").show();
			$("#fill-color").show();
			App.setEnableKineticCreationMode(true,false);
			$("#stroke-color").removeClass('active');
			$("#fill-color").removeClass('active');
			App.toolbox.editMode = false;
			App.toolbox.editedProp = false;
			App.toolbox.editedId = false;
			$("#toolbox-title").html(App.localize("%editing_tools"));
			$('.stroke-size option:first-child').attr("selected", "selected");
			App.kinetic.shape.config.setStrokeWidth("1");
			$('.stroke-style option:first-child').attr("selected", "selected");
			App.kinetic.shape.config.setStrokeStyle("normal");
			
		},
		
		
		showForPencil : function(){
			App.setCreationObject("shape","pencil");
			$( "#shape-dragable-menu" ).show();
			$(".shape-2").hide();
			$("#fill-color").hide();
			App.setEnableKineticCreationMode(true,false);
			$("#stroke-color").removeClass('active');
			$("#fill-color").removeClass('active');
			App.toolbox.editMode = false;
			App.toolbox.editedProp = false;
			App.toolbox.editedId = false;
			$("#toolbox-title").html(App.localize("%pencil_tools"));
			$('.stroke-size option:first-child').attr("selected", "selected");
			App.kinetic.shape.config.setStrokeWidth("1");
			$('.stroke-style option:first-child').attr("selected", "selected");
			App.kinetic.shape.config.setStrokeStyle("normal");
		},
		
		showForEdit : function(objId){
			App.toolbox.editMode = true;
			App.toolbox.editedId = objId;
			var prevObj = $("#"+App.toolbox.editedId);
			App.toolbox.editedProp={
				x:prevObj.position().left,
				y:prevObj.position().top,
				width:prevObj.width(),
				height:prevObj.height(),
			};
			$( "#shape-dragable-menu" ).show();
			App.setEnableKineticEditCreationMode(true,false);
			$("#stroke-color").removeClass('active');
			$("#fill-color").removeClass('active');
			
			var width = $("#draw_body").css('width');
			width=(parseInt(width.substring(0,width.length-2))-250);
			$( "#shape-dragable-menu").css('left', width);
			$( "#shape-dragable-menu").css('top', 0);
			$(".shape-2").show();
			$("#fill-color").show();
			$("#stroke-color").removeClass('active');
			$("#fill-color").removeClass('active');
			$("#toolbox-title").html(App.localize("%editing_tools"));
			$('.stroke-size option:first-child').attr("selected", "selected");
			App.kinetic.shape.config.setStrokeWidth("1");
			$('.stroke-style option:first-child').attr("selected", "selected");
			App.kinetic.shape.config.setStrokeStyle("normal");
		},
		
        init: function (e) {
			$('.jPicker').jPicker(
					{
					 window:
						  {
							expandable: true
						  },
						  images:
						  {
							clientPath : App.baseUrl+'assets/css/images/jPicker/'
						  }
					},
					function(color, context)
					{
					  var all = color.val('all');
					  var selectedColorPicker = $(this).parent().find(">:first-child").attr("id");
					  if(selectedColorPicker=="shapeJPicker")
					  {
						// $("#stroke-color").removeClass().addClass($(this).attr('class'));
						$("#stroke-color").removeClass('active');
						$("#fill-color").removeClass('active');
						var selectedCol = '#' + all.hex;
						$("#stroke-color").attr('sel-color',selectedCol);
						$("#stroke-color").css("background",selectedCol);
						App.kinetic.shape.config.setEditColor(selectedCol);
						console.log("Picker shape selected");
					  }
					  else if(selectedColorPicker =="stickyJPicker")
					  {
					  }
					}
			);		
			$(".floating-tools-pen").live('click', function(data){
			if(!App.toolboxValidation("tool_pencil_ico"))
			{
				return
			}
			
			var width = $("#draw_body").css('width');
			width=(parseInt(width.substring(0,width.length-2))-250);
			$( "#shape-dragable-menu").css('left', width);
			$( "#shape-dragable-menu").css('top', 0);
			App.toolbox.showForPencil();
			App.creationKind = "pencil";
			
			$(this).addClass('active');
			App.openedTool = $(this);
			
		});
		$('.floating-tools').live('click', function(data){
			if(!App.toolboxValidation("tool_shape_ico"))
			{
				return
			}
			App.toolbox.show();
			App.creationKind = "shape";
			$(this).addClass('active');
			App.openedTool = $(this);
		});
			$("#shape-dragable-menu").draggable({cancel:'select,#trans_value_text,#slider_cont'});//{cancel:'.shape-2,.btn-color,.cancel_drag'});
			
			/*$('.shape-2 .btn-color .cancel_drag').on( 'click', 'input', function () {    
				$(this).focus();
			});*/
			
			$("#shape-dragable-menu").hide();
			$("#creation-pencil").addClass('active');
			$(".button").button();
			
			$(".btn-color").click(function() {
				// $("#stroke-color").removeClass().addClass($(this).attr('class'));
				//$("#stroke-color").removeClass('active');
				//$("#fill-color").removeClass('active');
				var selectedColor = $(this).attr('data-color');
				if(App.toolbox.frameMode){
				$("#stroke-color").css("background",selectedColor);
				$("#stroke-color").attr('sel-color',selectedColor);
				App.kinetic.shape.config.setEditColor(selectedColor);
				}else{
				$("#fill-color").css("background",selectedColor);
				$("#fill-color").attr('sel-color',selectedColor);
				App.kinetic.shape.config.setEditFillColor(selectedColor);
				}
			});
			
			$("#stroke-color").click(function() {
				App.toolbox.frameMode = true;
				
				$("#stroke-color").addClass('active');
				$("#fill-color").removeClass('active');
	
			});
			
			$("#fill-color").click(function() {
				App.toolbox.frameMode = false;
			
				$("#stroke-color").removeClass('active');
				$("#fill-color").addClass('active');
				
			});
					
			$("#creation-pencil").click(function() {
				App.setCreationObject("shape","pencil");
			});
			
			$("#creation-line").click(function() {
				App.setCreationObject("shape","line");
				App.kinetic.shape.config.setActiveArrowMode(false);
			});
			
			$("#creation-arrow").click(function() {
				App.setCreationObject("shape","line");
				App.kinetic.shape.config.setActiveArrowMode(true);
			});
			
			$("#creation-circle").click(function() {
				App.kinetic.shape.config.setActiveFillMode(false);
				App.setCreationObject("shape","circle");
			});
			
			$("#creation-circle-fill").click(function() {
				App.kinetic.shape.config.setActiveFillMode(true);
				App.setCreationObject("shape","circle");
			});
			
			$("#creation-rect").click(function() {
				App.kinetic.shape.config.setActiveFillMode(false);
				App.kinetic.shape.config.setActiveRoundedMode(false);
				App.setCreationObject("shape","rect");
			});
			
			$("#creation-rect-fill").click(function() {
				App.kinetic.shape.config.setActiveFillMode(true);
				App.kinetic.shape.config.setActiveRoundedMode(false);
				App.setCreationObject("shape","rect");
			});
			
			$("#creation-rect-round").click(function() {
				App.kinetic.shape.config.setActiveFillMode(false);
				App.kinetic.shape.config.setActiveRoundedMode(true);
				App.setCreationObject("shape","rect");
			});
			
			$("#creation-rect-round-fill").click(function() {
				App.kinetic.shape.config.setActiveFillMode(true);
				App.kinetic.shape.config.setActiveRoundedMode(true);
				App.setCreationObject("shape","rect");
			});
	
			
			$(".stroke-size").change(function() {
				App.kinetic.shape.config.setStrokeWidth($(this).val());
			});
			
			$(".stroke-style").change(function() {
				App.kinetic.shape.config.setStrokeStyle($(this).val());
			});
			
			$(".save-button").click(function() {
				App.openedTool.removeClass('active');
				App.openedTool =false;
				//Firman : Hide Color Picker
				$('.jPicker').hide();
				$( "#shape-dragable-menu" ).hide();
				if(App.toolbox.editMode)
					App.setEnableKineticEditCreationMode(false,true);
				else
					App.setEnableKineticCreationMode(false,true);
			});
			
			$(".cancel-button").click(function() {
				
				var r=confirm(App.localize("%cancel_confirmation"));
				if (r==true)
				{  
					//Firman : Hide Color Picker
					$('.jPicker').hide();
					App.openedTool.removeClass('active');
					App.openedTool =false;
					$( "#shape-dragable-menu" ).hide();
					if(App.toolbox.editMode)
						App.setEnableKineticEditCreationMode(false,false);
					else
						App.setEnableKineticCreationMode(false,false);
						
					//hide any color picker windows if shown.
					$('.jPicker.Container').each(function(){
						if($(this).css('display') == 'block')
						{	
							$(this).css('display','none');
						}
					});	
				}
				
			});
			
			$(".undo_cont").click(function() {
				App.kinetic.undo();
			});
			
			$(".redo_cont").click(function() {
				App.kinetic.redo();
			});
			// DISABLE slider alpha,ganti button + textfield,RHEO
			// if( !(navigator.userAgent.match(/Android/i)
					// || navigator.userAgent.match(/webOS/i)
					// || navigator.userAgent.match(/iPhone/i)
					// || navigator.userAgent.match(/iPad/i)
					// || navigator.userAgent.match(/iPod/i)
					// || navigator.userAgent.match(/BlackBerry/i)
					// || navigator.userAgent.match(/Windows Phone/i)
				  // )){
				// $("#slider").kendoSlider({
					// change: App.toolbox.sliderOnChange,
					// slide: App.toolbox.sliderOnSlide,
					// min: 0,
					// max: 100,
					// width:"100%",
					// smallStep: 1,
					// largeStep: 5,
					// value: 100
				// });
			// }else{
				$("#trans_value").hide();
			// }
			
			$(".dec_alpha").click(function(){
				if($("#trans_value_text").val()>0){
					$("#trans_value_text").val(parseInt($("#trans_value_text").val())-5);
					App.kinetic.alpha = $("#trans_value_text").val();
					App.kinetic.shape.config.alpha = $("#trans_value_text").val();
				}
			});
			$("#trans_value_text").on('input',function(e){
				var val = $("#trans_value_text").val();
				if(isNaN(val))
				{
					$("#trans_value_text").val("100");
				}
				else if(parseInt(val)>100)
				{
					$("#trans_value_text").val("100");
				}
				else if(parseInt(val)<0)
				{
					$("#trans_value_text").val("0");
				}
				App.kinetic.alpha = parseInt($("#trans_value_text").val());
				App.kinetic.shape.config.alpha =  parseInt($("#trans_value_text").val());
			});
			$(".add_alpha").click(function(){
				if($("#trans_value_text").val()<100){
					$("#trans_value_text").val(parseInt($("#trans_value_text").val())+5);
					App.kinetic.alpha = $("#trans_value_text").val();
					App.kinetic.shape.config.alpha = $("#trans_value_text").val();
				}
			});
			// $("#trans_value_text").attr('readonly', true);
			
		},
		
		sliderOnSlide : function(e) {
			$("#trans_value").html(parseInt(100*e.value/100) + "%");
			App.kinetic.alpha = e.value;
			App.kinetic.shape.config.alpha = e.value;
			if(App.kinetic.lastCreation!=null){
				App.kinetic.lastCreation.setOpacity(parseInt(100*e.value/100)/100.0);
				App.kinetic.layer.draw();
			}
		},

		sliderOnChange	: function(e) {
			$("#trans_value").html(parseInt(100*e.value/100) + "%");
			App.kinetic.alpha = e.value;
			App.kinetic.shape.config.alpha = e.value;
			
		}
	}
});